import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../pages/Login.vue'
// import Home from '../pages/Home.vue'
// import Welcome from '../pages/Welcome.vue'
// import Users from '../pages/user/Users.vue'
// import Rights from '../pages/power/Rights.vue'
// import Roles from '../pages/power/Roles.vue'
// import Categories from '../pages/goods/Categories.vue'
// import Params from '../pages/goods/Params.vue'
// import Goods from '../pages/goods/Goods.vue'
// import Add from '../pages/goods/Add.vue'
// import Order from '../pages/order/Order.vue'
// import Reports from '../pages/reports/Reports.vue'

const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '../pages/Login.vue')
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '../pages/Home.vue')
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '../pages/Welcome.vue')

const Users = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../pages/user/Users.vue')
const Rights = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../pages/power/Rights.vue')
const Roles = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../pages/power/Roles.vue')

const Categories = () => import(/* webpackChunkName: "Categories_Params" */ '../pages/goods/Categories.vue')
const Params = () => import(/* webpackChunkName: "Categories_Params" */ '../pages/goods/Params.vue')

const Goods = () => import(/* webpackChunkName: "Goods_Add" */ '../pages/goods/Goods.vue')
const Add = () => import(/* webpackChunkName: "Goods_Add" */ '../pages/goods/Add.vue')

const Order = () => import(/* webpackChunkName: "Order_Report" */ '../pages/order/Order.vue')
const Reports = () => import(/* webpackChunkName: "Order_Report" */ '../pages/reports/Reports.vue')

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      redirect: '/welcom',
      children: [
        { path: '/welcom', component: Welcome },
        { path: '/users', component: Users },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Categories },
        { path: '/params', component: Params },
        { path: '/goods', component: Goods },
        { path: '/goods/add', component: Add },
        { path: '/orders', component: Order },
        { path: '/reports', component: Reports}
    ] },

  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next();
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login');
  next()
})

export default router
